package com.microcity.cleanjava.mvvm.ui.map.activity;

import android.Manifest;
import android.arch.lifecycle.ViewModel;
import android.content.Intent;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.widget.TextView;

import com.microcity.cleanjava.R;
import com.darksider.basemodule.helper.RxHelper;
import com.darksider.basemodule.base.originbase.OriginBaseActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class FlashActivity extends OriginBaseActivity<ViewDataBinding, ViewModel> {

    private boolean mIsCancle;
    private int mTime = 3;
    private TextView mTvCountDown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTvCountDown = findViewById(R.id.tv_count_down);
        mTvCountDown.setOnClickListener(v -> {
            mIsCancle = true;
            finish();
            startActivity(new Intent(FlashActivity.this, MainActivity.class));
        });
        initPermission();

    }

    private void initPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions
                .request(Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA)
                .subscribe(granted -> {
                    if (granted) {
                        initCountDown();
                    } else {
                        finish();
                        System.exit(0);
                    }
                });
    }

    private void initCountDown() {

        Observable.interval(1, TimeUnit.SECONDS)
                .take(3)//计时次数
                .map(aLong -> {
                    return mTime - aLong;// 3-0 3-2 3-1
                })
                .compose(RxHelper.rxObservableSchedulerHelper())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Long value) {
                        mTvCountDown.setText(String.valueOf(value));
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        if (!mIsCancle) {
                            startActivity(new Intent(FlashActivity.this, MainActivity.class));
                            finish();
                        }
                    }
                });
    }

    @Override
    public int getContentView() {
        return R.layout.activity_flash;
    }

}
